home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: jlilley@ix.netcom.com (John Lilley)
- Newsgroups: comp.lang.c++
- Subject: Re: Multiple Inheritance Pointer Problem
- Date: 3 Apr 1996 15:04:12 GMT
- Organization: Netcom
- Message-ID: <4ju41c$96q@cloner2.ix.netcom.com>
- References: <31622A26.3633@nmaa.org>
- NNTP-Posting-Host: den-co11-06.ix.netcom.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-NETCOM-Date: Wed Apr 03 7:04:12 AM PST 1996
- X-Newsreader: WinVN 0.99.7
-
- In article <31622A26.3633@nmaa.org>, jphelps@nmaa.org says...
- >
- >I recently used multiple inheritence to help me create a few
- >socket classes. The details of these socket classes are not
- >important; however, in my pursual of bugs I found that some
- >of the pointers to these classes caused core dumps while
- >other classes seem to work fine. So, as part of my debug
- >process, I printed out pointers to theses classes before I
- >passed them around, and then printed these pointers after I
- >passed them in to various functions, and to my surprise some
- >of these pointers were off by four bytes! (This took place on
- >an HP 735, running HP-UX 9.03 -- I think these environment
- >specs are correct; its late and I'm groggy.)
- >
- >
- >#include <iostream.h>
- >
- >class foo {
- >public:
- > void PrintMe(void) { cerr << "this = " << this << endl; }
- >};
- >
- >class bar
- >{
- >#ifdef OFF_BY_4
- >public:
- > int b;
- >#endif
- >};
- >
- >class foobar : public foo, public bar
- >{
- >#ifdef OFF_BY_4
- >public:
- > int fb;
- >#endif
- >};
- >
- >class barfoo : public bar, public foo
- >{
- >#ifdef OFF_BY_4
- >public:
- > int bf;
- >#endif
- >};
- >
- >main()
- >{
- > foobar fb;
- > barfoo bf;
- >
- > cerr << "&fb = " << &fb << endl;
- > fb.PrintMe();
- > cerr << "&bf = " << &bf << endl;
- > bf.PrintMe();
- >
- > return(0);
- >}
- >
- >&fb = 0x0012FFAC
- >this = 0x0012FFAC
- >&bf = 0x0012FFA8
- >this = 0x0012FFA9
- >...
-
-
- In multiple inheritance, the address of a base class may not be
- the same as the address of a derived class. This is true because
- more than one base cannot be placed at the same physical address
- when composed to form the derived object.
-
- What you are printing out is:
-
- Address of foobar
- cerr << "&fb = " << &fb << endl;
- Address of "foo" part of foobar
- fb.PrintMe();
- Address of barfoo
- cerr << "&bf = " << &bf << endl;
- Address of "foo" part of barfoo
- bf.PrintMe();
-
- Your output is sensible since (1) The compiler may arrange the order of
- multiple bases any way it likes, (2) foobar and bar probably have the foo and
- bar portions arranged differently.
-
- john lilley
-
-